<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item v-for="(item,index) in pageList" :key="'breadcrumb'+index" class="breadcrumbStyle" @click.native="goPage(item,index)">
        {{ item.title || item.defaultTitle }}
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider />
    <component :is="activeName" :tochild="toChild" @child2Far="childJump" />

  </el-card>
</template>

<script>

import regionManage from '@/views/back-management/account-set/regionManage'
import hospitalManage from '@/views/back-management/account-set/hospitalManage'
import centerManage from '@/views/back-management/account-set/centerManage'
import basicManage from '@/views/back-management/account-set/basicManage'

import { getHospitalListNew } from '@/api/back-management/account-set/hospitalManage'
export default {
  name: 'AccountSet',
  components: { regionManage, hospitalManage, basicManage, centerManage },
  data() {
    return {
      activeName: '',
      pageList: [],
      toChild: {},
      pageMap: {
        region: {
          id: '',
          title: '',
          fileName: 'regionManage',
          defaultTitle: '区域管理',
          data: null
        },
        hospital: {
          id: '',
          title: '',
          fileName: 'hospitalManage',
          defaultTitle: '医院管理',
          data: null
        },
        center: {
          id: '',
          title: '',
          fileName: 'centerManage',
          defaultTitle: '中心医院',
          data: null
        },
        basic: {
          id: '',
          title: '',
          fileName: 'basicManage',
          defaultTitle: '基层医院',
          data: null
        }
      }
    }
  },

  created() {
    this.pageForRole()
    // 添加刷新监听,刷新后保持tab页选中
    // window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
    // this.activeName = sessionStorage.getItem('activeName') || this.activeName
  },
  beforeDestroy() {
    // 卸载监听事件
    // window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
    // 清除sessionStorage
    // sessionStorage.removeItem('activeName')
  },
  methods: {
    childJump(data) {
      // console.log('来自子组件数据', data)
      this.pageMap[data.toPage].title = data.childTitle
      this.pageMap[data.toPage].data = data.data
      this.pageList.push(this.pageMap[data.toPage])
      this.pageModify(this.pageMap[data.toPage])
    },
    pageModify(data) {
      this.toChild = data
      this.activeName = data.fileName
    },
    goPage(item, index) {
      if (item.fileName !== this.activeName) {
        // 更改面包屑长度
        this.pageList.splice(parseInt(index) + 1)
        this.pageModify(item)
      }
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },

    // 根据角色判断进入的页面
    pageForRole() {
      if (this.$store.getters.sysRole.includes('超级管理员')) {
        this.pageList = [this.pageMap.region]
        this.activeName = 'regionManage'
      } else {
        this.getRoleHospitalInfo()
      }
    },

    // 判断当前医院是中心医院还是基层医院
    getRoleHospitalInfo() {
      const data = {
        token: this.$store.getters.token,
        type: '',
        level: '',
        hospitalId: this.$store.getters.hospitalId
      }
      getHospitalListNew(data)
        .then(res => {
          const { type, regionId, name } = res.data.list[0]
          const pageData = {
            hospitalId: this.$store.getters.hospitalId,
            regionId: regionId.toString()
          }
          const typeinfo = type ? type : '1'
          if (typeinfo === '1') {
            this.pageMap.center.data = pageData
            this.pageMap.center.title = name
            this.toChild = this.pageMap.center
            this.activeName = this.pageMap.center.fileName
            this.pageList = [this.pageMap.center]
          } else if (typeinfo === '2') {
            this.pageMap.basic.data = pageData
            this.pageMap.basic.title = name
            this.toChild = this.pageMap.basic
            this.activeName = this.pageMap.basic.fileName
            this.pageList = [this.pageMap.basic]
          } else {
            this.$message.error('查询当前医院信息错误')
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

  <style scoped>
/*    #box {
      background-color: #f2f2f2;
      width: 100%;
      height: 100%;
    } */
    .breadcrumbStyle{
      cursor: pointer;
    }
    .addBtn{
      position: relative;
      top: -80px;
    }
  </style>
